<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <nav class="nav-bar">
      <div class="logo">
        <el-icon :size="32"><Monitor /></el-icon>
        <span>儿童健康伙伴</span>
        <div class="logo-decoration"></div>
      </div>
      
      <!-- 主导航 -->
      <div class="nav-items">
        <router-link to="/user/home" class="nav-item">
          <el-icon><HomeFilled /></el-icon>
          <span>首页</span>
        </router-link>
        <router-link to="/user/growth" class="nav-item">
          <el-icon><TrendCharts /></el-icon>
          <span>成长记录</span>
        </router-link>
        <router-link to="/user/health" class="nav-item">
          <el-icon><User /></el-icon>
          <span>健康档案</span>
        </router-link>
        <router-link to="/user/education" class="nav-item">
          <el-icon><Reading /></el-icon>
          <span>健康科普</span>
        </router-link>
        <router-link to="/user/assessment" class="nav-item">
          <el-icon><DataAnalysis /></el-icon>
          <span>心理测评</span>
        </router-link>
        <router-link to="/user/consult" class="nav-item">
          <el-icon><Service /></el-icon>
          <span>咨询专家</span>
        </router-link>
        <router-link to="/user/community" class="nav-item">
          <el-icon><ChatDotRound /></el-icon>
          <span>育儿社区</span>
        </router-link>
        <router-link to="/user/selfcheck" class="nav-item">
          <el-icon><ChatDotRound /></el-icon>
          <span>自检助手</span>
        </router-link>
        <router-link to="/user/hospitalNav" class="nav-item">
          <el-icon><ChatDotRound /></el-icon>
          <span>医院导航</span>
        </router-link>
      </div>

      <!-- 用户信息 -->
      <div class="user-section">
        <el-dropdown>
          <div class="user-info">
            <el-avatar :size="36" :src="userInfo.avatar || defaultAvatar" />
            <span>{{ userInfo.nickname || userInfo.username }}</span>
            <el-icon><ArrowDown /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleProfile">
                <el-icon><UserFilled /></el-icon>
                我的信息
              </el-dropdown-item>
              <el-dropdown-item @click="handleSettings">
                <el-icon><Setting /></el-icon>
                个人设置
              </el-dropdown-item>
              <el-dropdown-item @click="handleLogout">
                <el-icon><SwitchButton /></el-icon>
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </nav>

    <!-- 主内容区 -->
    <main class="main-content">
      <router-view></router-view>
    </main>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { 
  HomeFilled, 
  User, 
  TrendCharts, 
  Service, 
  Monitor, 
  Reading,
  ArrowDown,
  UserFilled,
  SwitchButton,
  ChatDotRound,
  Setting,
  DataAnalysis
} from '@element-plus/icons-vue'
import { getCurrentUsername, removeToken } from '@/utils/auth'
import { getUserInfo } from '@/api/user'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()

const defaultAvatar = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
const userInfo = ref({
  username: '',
  nickname: '',
  avatar: ''
})

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    const data = await getUserInfo()
    userInfo.value = data
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
}

const handleProfile = () => {
  router.push('/user/profile')
}

const handleSettings = () => {
  router.push('/user/settings')
}

const handleLogout = () => {
  removeToken()
  ElMessage.success('退出成功')
  router.push('/login')
}

onMounted(() => {
  fetchUserInfo()
})
</script>

<style scoped>
.app-container {
  min-height: 100vh;
  background: linear-gradient(to bottom, #f7f9fc, #ffffff);
}

.nav-bar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  height: 70px;
  padding: 0 32px;
  display: flex;
  align-items: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 100;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 22px;
  color: var(--el-color-primary);
  font-weight: bold;
  position: relative;
}

.logo .el-icon {
  margin-right: 12px;
  color: var(--el-color-primary);
}

.logo-decoration {
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, var(--el-color-primary), transparent);
  border-radius: 2px;
}

.nav-items {
  display: flex;
  margin-left: 60px;
  gap: 36px;
}

.nav-item {
  display: flex;
  align-items: center;
  color: #666;
  text-decoration: none;
  font-size: 16px;
  padding: 10px 20px;
  border-radius: 25px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.nav-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--el-color-primary);
  opacity: 0;
  transform: translateY(100%);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: -1;
}

.nav-item:hover::before {
  transform: translateY(0);
  opacity: 0.1;
}

.nav-item .el-icon {
  margin-right: 8px;
  font-size: 20px;
  transition: transform 0.3s ease;
}

.nav-item:hover .el-icon {
  transform: translateY(-2px);
}

.nav-item span {
  font-weight: 500;
}

.nav-item.router-link-active {
  background: var(--el-color-primary);
  color: white;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

.nav-item.router-link-active .el-icon {
  transform: scale(1.1);
}

.main-content {
  padding-top: 90px;
  max-width: 1200px;
  margin: 0 auto;
  padding-left: 24px;
  padding-right: 24px;
}

.user-section {
  margin-left: auto;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  padding: 6px 16px;
  border-radius: 25px;
  transition: all 0.3s;
  border: 1px solid transparent;
}

.user-info:hover {
  background: #f0f7ff;
  border-color: var(--el-color-primary-light-7);
}

.user-info span {
  color: #333;
  font-size: 15px;
  font-weight: 500;
}

.user-info .el-icon {
  font-size: 12px;
  color: #666;
  transition: transform 0.3s;
}

.user-info:hover .el-icon {
  transform: rotate(180deg);
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  font-size: 16px;
}

:deep(.el-avatar) {
  border: 2px solid var(--el-color-primary-light-8);
  transition: transform 0.3s;
}

.user-info:hover :deep(.el-avatar) {
  transform: scale(1.05);
}
</style> 